package org.create.utils;

import java.io.*;

import static org.create.bean.Constants.FILE_ENCODING;

public class IoTool {
    private static BufferedWriter bw;
    private static OutputStreamWriter outputStreamWriter;
    private static FileOutputStream fileOutputStream;

    public static BufferedWriter createBw(File file) {
        try {
            fileOutputStream = new FileOutputStream(file);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream, FILE_ENCODING);
            bw = new BufferedWriter(outputStreamWriter);
        } catch (IOException e) {
            LogTool.printError(e);
            throw new RuntimeException(e.getMessage());
        }
        return bw;
    }

    public static void close() {
        try {
            if (bw != null) {
                bw.close();
            }
        } catch (IOException e) {
            LogTool.printError(e);
        }
        try {
            if (outputStreamWriter != null) {
                outputStreamWriter.close();
            }
        } catch (IOException e) {
            LogTool.printError(e);
        }
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            LogTool.printError(e);
        }
    }

    public static void close(BufferedOutputStream bo, FileOutputStream fileOutputStream, BufferedInputStream bi, FileInputStream fileInputStream) throws IOException {
        bo.close();
        fileOutputStream.close();
        bi.close();
        fileInputStream.close();
    }

    public static void copy(String source, String target) throws IOException {
        copy(source, target, 4080, true);
    }

    public static void copy(String source, String target, int buffSize) throws IOException {
        copy(source, target, buffSize, true);
    }

    public static void copy(String source, String target, int buffSize, boolean append) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(source);
        BufferedInputStream bi = new BufferedInputStream(fileInputStream);

        FileOutputStream fileOutputStream = new FileOutputStream(target, append);
        BufferedOutputStream bo = new BufferedOutputStream(fileOutputStream);

        byte[] buff = new byte[buffSize];
        int len;
        while ((len = bi.read(buff, 0, buff.length)) != -1) {
            bo.write(buff, 0, len);
        }
        close(bo, fileOutputStream, bi, fileInputStream);
    }

    public static void copy(File source, String target) throws IOException {
        copy(source.getPath(), target, 4080, true);
    }

    public static void copy(File source, String target, int buffSize) throws IOException {
        copy(source.getPath(), target, buffSize, true);
    }

    public static void copy(File source, String target, int buffSize, boolean append) throws IOException {
        copy(source.getPath(), target, buffSize, append);
    }

    public static void copy(String source, File target) throws IOException {
        copy(source, target.getPath(), 4080, true);
    }

    public static void copy(String source, File target, int buffSize) throws IOException {
        copy(source, target.getPath(), buffSize, true);
    }

    public static void copy(String source, File target, int buffSize, boolean append) throws IOException {
        copy(source, target.getPath(), buffSize, append);
    }

    public static void copy(File source, File target) throws IOException {
        copy(source, target.getPath(), 4080, true);
    }

    public static void copy(File source, File target, int buffSize) throws IOException {
        copy(source, target.getPath(), buffSize, true);
    }

    public static void copy(File source, File target, int buffSize, boolean append) throws IOException {
        copy(source, target.getPath(), buffSize, append);
    }
}
